package logger

import (
	"github.com/sirupsen/logrus"
	"gopkg.in/natefinch/lumberjack.v2"
	"io"
	"os"
)

// Logger 是封装后的日志接口
type Logger interface {
	Debug(args ...interface{})
	Debugf(format string, args ...interface{})
	Info(args ...interface{})
	Infof(format string, args ...interface{})
	Warn(args ...interface{})
	Warnf(format string, args ...interface{})
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	Panic(args ...interface{})
	Panicf(format string, args ...interface{})
	WithField(key string, value interface{}) Logger
	WithFields(fields map[string]interface{}) Logger
}

// LogConfig 日志配置
type LogConfig struct {
	Level      string `json:"level" yaml:"level"`            // 日志级别: trace, debug, info, warn, error, fatal, panic
	Filename   string `json:"filename" yaml:"filename"`      // 日志文件路径
	MaxSize    int    `json:"maxSize" yaml:"max_size"`       // 单个日志文件最大大小(MB)
	MaxBackups int    `json:"maxBackups" yaml:"max_backups"` // 保留的旧日志文件最大数量
	MaxAge     int    `json:"maxAge" yaml:"max_age"`         // 保留旧日志文件的最大天数
	Compress   bool   `json:"compress" yaml:"compress"`      // 是否压缩旧日志文件
	Console    bool   `json:"console" yaml:"console"`        // 是否输出到控制台
	Caller     bool   `json:"caller" yaml:"caller"`          // 是否显示调用者信息
}

// logrusLogger 是 Logger 接口的实现
type logrusLogger struct {
	*logrus.Entry
}

var (
	defaultLogger Logger
)

// init 初始化默认日志
func init() {
	Init(LogConfig{
		Level:    "info",
		Console:  false,
		Caller:   true,
		Filename: "",
	})
}

// Init 初始化日志系统
func Init(config LogConfig) {
	logrus.SetReportCaller(false) // 这行是关键修改
	// 设置日志级别
	level, err := logrus.ParseLevel(config.Level)
	if err != nil {
		level = logrus.InfoLevel
	}
	logrus.SetLevel(level)

	// 设置日志格式
	logrus.SetFormatter(&logrus.JSONFormatter{
		TimestampFormat: "2006-01-02 15:04:05",
	})

	// 设置输出
	var writers []io.Writer
	if config.Console {
		writers = append(writers, os.Stdout)
	}

	if config.Filename != "" {
		writers = append(writers, &lumberjack.Logger{
			Filename:   config.Filename,
			MaxSize:    config.MaxSize,
			MaxBackups: config.MaxBackups,
			MaxAge:     config.MaxAge,
			Compress:   config.Compress,
		})
	}

	if len(writers) > 0 {
		logrus.SetOutput(io.MultiWriter(writers...))
	}

	// 设置默认logger
	defaultLogger = &logrusLogger{logrus.NewEntry(logrus.StandardLogger())}
}

// Default 获取默认logger
func Default() Logger {
	return defaultLogger
}

// New 创建新的logger实例
func New() Logger {
	return &logrusLogger{logrus.NewEntry(logrus.StandardLogger())}
}

// 实现 Logger 接口方法
func (l *logrusLogger) WithField(key string, value interface{}) Logger {
	return &logrusLogger{l.Entry.WithField(key, value)}
}

func (l *logrusLogger) WithFields(fields map[string]interface{}) Logger {
	return &logrusLogger{l.Entry.WithFields(fields)}
}

// 以下是日志级别方法
func (l *logrusLogger) Debug(args ...interface{}) {
	l.Entry.Debug(args...)
}

func (l *logrusLogger) Debugf(format string, args ...interface{}) {
	l.Entry.Debugf(format, args...)
}

func (l *logrusLogger) Info(args ...interface{}) {
	l.Entry.Info(args...)
}

func (l *logrusLogger) Infof(format string, args ...interface{}) {
	l.Entry.Infof(format, args...)
}

func (l *logrusLogger) Warn(args ...interface{}) {
	l.Entry.Warn(args...)
}

func (l *logrusLogger) Warnf(format string, args ...interface{}) {
	l.Entry.Warnf(format, args...)
}

func (l *logrusLogger) Error(args ...interface{}) {
	l.Entry.Error(args...)
}

func (l *logrusLogger) Errorf(format string, args ...interface{}) {
	l.Entry.Errorf(format, args...)
}

func (l *logrusLogger) Fatal(args ...interface{}) {
	l.Entry.Fatal(args...)
}

func (l *logrusLogger) Fatalf(format string, args ...interface{}) {
	l.Entry.Fatalf(format, args...)
}

func (l *logrusLogger) Panic(args ...interface{}) {
	l.Entry.Panic(args...)
}

func (l *logrusLogger) Panicf(format string, args ...interface{}) {
	l.Entry.Panicf(format, args...)
}
